﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
using Models;
using System.IO;

namespace Common
{
  public class NoteList
  {
    public string label { get; set; }
    public string id { get; set; }
    public List<NoteItem> noteItems { get; set; }
  }


  public class NoteItem
  {
    public string id { get; set; }
    public bool isFocus { get; set; }
    public Position position { get; set; }
    public List<Line> lines { get; set; }
    public string title { get; set; }
  }

  public class Position
  {
    public int left { get; set; }
    public int top { get; set; }
  }

  public class Line
  {
    public string content { get; set; }
    public int rate { get; set; }
  }
  public class JsonFileHelper
  {
    public static string GetItem(string id = "")
    {
      try
      {
        var jsonStr = File.ReadAllText(Path.Combine("notelis", id + ".json"), Encoding.UTF8);
        return jsonStr;
      }
      catch (Exception e)
      {
        return "";
      }
    }
    public static bool Update(string id, string content)
    {
      try
      {
        string fname = Path.Combine("notelis", id + ".json");

        if (!File.Exists(fname))
        {
          FileStream f = new FileStream(fname, FileMode.Create, FileAccess.ReadWrite);
          f.Close();
        }

        File.WriteAllText(fname, content);
        return true;
      }
      catch (Exception e)
      {
        return false;
      }
    }

    public static List<string> GetAccountList()
    {
      string[] files = Directory.GetFiles("notelis", "*.json");
      var res = new List<string>();
      foreach (var path in files) res.Add(Path.GetFileName(path).Split(".")[0]);
      return res;
    }

    public static string GetObs(string name)
    {
      try
      {
        var jsonStr = File.ReadAllText(Path.Combine("obs", name + ".json"), Encoding.UTF8);
        return jsonStr;
      }
      catch (Exception e)
      {
        return "";
      }
    }

    public static bool UpdateObs(string name, string content)
    {
      try
      {
        string fname = Path.Combine("obs", name + ".json");

        if (!File.Exists(fname))
        {
          FileStream f = new FileStream(fname, FileMode.Create, FileAccess.ReadWrite);
          f.Close();
        }

        File.WriteAllText(fname, content);
        return true;
      }
      catch (Exception e)
      {
        return false;
      }
    }
  }
}
